<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/24
 * Time: 14:28
 */
namespace Admin\Controller;


use Admin\Model\DoctorInfoModel;
use Admin\Model\PatientInfoModel;
use Base\Controller\CurdController;
use Think\Page;

class MedicalInformationsController extends CurdController
{
    public function __construct()
    {
        parent::__construct(true, true, array(
                'input_name' => 'coverFile',//文本框name
                'save_path' => 'information',//保存路径
                'data_field' => 'cover'//数据库字段
            )
        );
    }
    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['title']  = array('like', '%'.$_REQUEST['keyword'].'%');
			$map['content']  = array('like', '%'.$_REQUEST['keyword'].'%');
			$map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
		if(I('get.del')){
			$where['status']=0;
		}else{
			$where['status']=1;
		}
        $data = $this->model->pageToView($limit, true,$where,'','id asc');
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }
    /**
     *
     * 根据用户类型获取用户数据
     * 1为医生，2为患者
     */
    public function  getuserLists(){
        $where['status']=1;
        switch ($_REQUEST['tpye_id']){
            case 1:
                    $doctormodel  =new DoctorInfoModel();
                    $datalist=$doctormodel->where($where)->select();
                break;
            case 2:
                    $PatientInfoModel =new PatientInfoModel();
                    $datalist=$PatientInfoModel->where($where)->select();
                break;
            default:
                return false;
                break;
        }
              $this->ajaxReturn($datalist);
    }

    /**
     * @param $data
     * @return mixed
     * 新增数据处理
     */
    protected function addAfter($data)
    {
        $data['content']=htmlspecialchars_decode($data['content']);
        return $data;
    }
    /**
     * @param $data
     * @return mixed
     * 编辑数据处理
     */
    protected function editAfter($data)
    {
        $data['content']=htmlspecialchars_decode($data['content']);
        return $data;
    }
	    /**
     * 删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function del($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $data['status']=0;
            $this->model->where('id=' . $v)->save($data);
            $i++;
        }
        $this->success('删除' . $i . '条数据', $this->urlConfig['del_success']);
    }
	    /**
     * 恢复删除
     * @param string $id 要恢复的id，多个以,分割
     */
    public function recover($id){
        $arr = explode(',', $id);
        $i = 0;

        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $data['status']=1;
            $this->model->where('id=' . $v)->data($data)->save();
            $i++;
        }
        $this->success('恢复' . $i . '条数据', $this->urlConfig['del_success']);
    }
 }